package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhentao.common.DateUtils;
import com.zhentao.common.dtos.ResponseResult;
import com.zhentao.common.enums.AppHttpCodeEnum;
import com.zhentao.constants.Constants;
import com.zhentao.constants.UserThreadLocal;
import com.zhentao.follow.domain.AppUserFollow;
import com.zhentao.mapper.AppUserInfoMapper;
import com.zhentao.mapper.AppUserMapper;
import com.zhentao.service.AppUserFollowService;
import com.zhentao.service.AppUserService;
import com.zhentao.service.UserSaveService;
import com.zhentao.user.domain.AppUser;
import com.zhentao.user.domain.AppUserInfo;
import com.zhentao.user.dto.UserInfoSaveDto;
import com.zhentao.user.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserSaveServiceImpl implements UserSaveService {
    @Resource
    private AppUserInfoMapper userInfoMapper;

    @Resource
    private AppUserMapper appUserMapper;

    @Autowired
    private AppUserFollowService appUserFollowService;

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public ResponseResult saveUserInfo(UserInfoSaveDto dto) {
        AppUserInfo info = new AppUserInfo();
        BeanUtils.copyProperties(dto,info);
        info.setCreateTime(new Date());
        AppUser userInfo = UserThreadLocal.getUserInfo();
        info.setUserId(userInfo.getId());
        info.setOrderBy(1);
        userInfoMapper.insert(info);
        return null;
    }

    @Override
    public ResponseResult getUserInfo(String openId) {
        UserVo user = appUserMapper.getUserInfo(openId);
        return ResponseResult.okResult(user);
    }

    @Override
    public ResponseResult forgetUserInfo(Integer id,Integer options) {
        AppUser userInfo = UserThreadLocal.getUserInfo();
        if(userInfo == null){
            return ResponseResult.errorResult(501,"未登录");
        }
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if(options==1){
            AppUserFollow upUserFollow = new AppUserFollow();
            upUserFollow.setUserId(userInfo.getId());
            upUserFollow.setFollowId(id);
            upUserFollow.setCreateTime(new Date());
            appUserFollowService.save(upUserFollow);
            redisTemplate.opsForSet().add(Constants.USER_FOLLOW +userInfo.getId(),id.toString());
            return ResponseResult.okResult("关注成功");
        }else if(options==2){
            appUserFollowService.remove(Wrappers.<AppUserFollow>lambdaQuery()
                    .eq(AppUserFollow::getUserId,userInfo.getId())
                    .eq(AppUserFollow::getFollowId, id));
            redisTemplate.opsForSet().remove(Constants.USER_FOLLOW +userInfo.getId(),id.toString());
            return ResponseResult.okResult("取关成功");
        }
        return ResponseResult.errorResult(501,"系统异常");
    }


}
